package com.hd.assistant.biz.manager.ordermining.concurrent;

import com.hd.assistant.biz.manager.exception.ManagerException;
import com.hd.assistant.biz.manager.ordermining.OrderMiningManager;
import com.hd.assistant.dal.dataobject.ordermining.NotifyMessage;
import com.hd.common.taobao.notify.MessageDecode;
import com.taobao.api.ApiException;
import com.taobao.api.domain.NotifyTopats;
import com.taobao.api.domain.NotifyTrade;

public class MessageInsertDBTask implements Runnable{
	OrderMiningManager orderMiningManager ;
	String message;
	public MessageInsertDBTask(OrderMiningManager orderMiningManager,
			String message) {
		super();
		this.orderMiningManager = orderMiningManager;
		this.message = message;
	}
	@Override
	public void run() {
		
		try {
			Object object = MessageDecode.decodeMsg(message);
			if(object==null)
				return;
			NotifyMessage mess = new NotifyMessage();
			mess.setStatus(NotifyMessage.STATUS_INIT);
			if(object instanceof NotifyTrade){
				NotifyTrade trade = (NotifyTrade)object;
				mess.setTopic(trade.getTopic());
				mess.setContentStatus(trade.getStatus());
				mess.launchMessageObject(trade);
			}
			else if(object instanceof NotifyTopats){
				NotifyTopats topats = (NotifyTopats)object;
				mess.setTopic(topats.getTopic());
				mess.setContentStatus(topats.getStatus());
				mess.launchMessageObject(topats);
			}
			
			this.orderMiningManager.insertNotifyMessage(mess);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ManagerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
